package com.cyanite.playlv2api.parsers.json;

import com.cyanite.playlv2api.types.User;

import org.json.JSONException;
import org.json.JSONObject;

public class UserParser extends AbstractParser<User> {

    @SuppressWarnings("unchecked")
    @Override
    public User parse(JSONObject json) throws JSONException {

        User user = new User();
        if (json.has("uid")) {
            user.setId(json.getString("uid"));
        }
        if (json.has("badges")) {
            user.setBadges(
                new GroupParser(
                    new BadgeParser()).parse(json.getJSONArray("badges")));
        }
        if (json.has("pms")) {
            user.setMsgCount(json.getInt("pms"));
        }
        if (json.has("rmb")) {
            user.setScore(json.getInt("rmb"));
        }
        if (json.has("requests")) {
            user.setFriendRequestCount(json.getInt("requests"));
        }
        if (json.has("checkin")) {
            user.setCheckinCount(json.getInt("checkin"));
        }
        if (json.has("email")) {
            user.setEmail(json.getString("email"));
        }
        if (json.has("focus")) {
            user.setFollowerCount(json.getInt("focus"));
        }
        if (json.has("friends")) {
            user.setFriendCount(json.getInt("friends"));
        }
        if (json.has("gender")) {
            user.setGender(json.getString("gender"));
        }
        if (json.has("nick")) {
            user.setUserName(json.getString("nick"));
        }
        if (json.has("phone")) {
            user.setPhone(json.getString("phone"));
        }
        if (json.has("avatar")) {
            user.setPhoto(json.getString("avatar"));
        }
        if (json.has("tips")) {
            user.setTipCount(json.getInt("tips"));
        }
        if (json.has("status")) {
            user.setFriendstatus(String.valueOf(json.getInt("status")));
        }
        if (json.has("users")) {
            user.setFriendsInCommon(
                new GroupParser(
                    new UserParser()).parse(json.getJSONArray("users")));
        }

        if (json.has("created")) {
            user.setCreated(json.getString("created"));
        }
        /*if (json.has("checkin")) {
            user.setCheckin(new CheckinParser().parse(json.getJSONObject("checkin")));
        }*/
        if (json.has("hometown")) {
            user.setHometown(json.getString("hometown"));
        }

        if (json.has("settings")) {
            user.setSettings(new SettingsParser().parse(json.getJSONObject("settings")));
        }
        return user;
    }

    //@Override
    //public String getObjectName() {
    //    return "user";
    //}
}